package demo1;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            if (map.containsKey(nums[i]) && Math.abs(i - map.get(nums[i])) <= k) {
                return true;
            }
            map.put(nums[i], i);
        }
        return false;
    }

    public int sumOfPower(int[] nums) {
        Arrays.sort(nums);
        long ans = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = i; j < nums.length; j++) {
                long sum = (nums[j] * nums[j]) % 1000000007L;
                sum = (sum * nums[i]) % 1000000007L;
                sum = (sum * (long) Math.pow(2, Math.max(j - i - 1, 0)) % 1000000007L) % 1000000007L;
                ans = (ans + sum) % 1000000007L;
            }
        }
        return (int) ans;
    }
}
